const appTable = require( '../models/app' );
const appUsageTable = require( '../models/app_usage' );

async function recordAppUsage( userId, appName ) {

    await appTable.plusUseCount( appName );

    try {

        var result = await appTable.getByName( appName );

        if ( result && result.length > 0 ) {
            var app = result[ 0 ];
            await appUsageTable.insert( {
                userId: userId,
                appId: app.id
            } );
        }
    } catch ( e ) {
        console.error( "记录app使用信息出错", e );
    }

}

exports.recordAppUser = recordAppUsage;
